import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {NavbarService} from './navbar.service';
import {BaseComponent} from '../common/base.component';
import {Authority} from './navbar';
import {AuthService} from '../user/auth.service';

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})
export class NavbarComponent extends BaseComponent implements OnInit {
  authority: Authority[];
  account: string;

  constructor(private router: Router,
              private navbarService: NavbarService,
              private authService: AuthService) {
    super();
  }

  ngOnInit() {
    this.crobot();
    this.account = this.authService.getAccountName();
  }

  logOut() {
    this.navbarService.logout()
      .then(res => {
        if (res[0]) {
          this.authService.clearTokenId();
          this.router.navigate(['/login']);
        }
      });
  }

  crobot() {
    this.navbarService.getRobot()
      .then(res => {
        if (res[0]) {
          this.authority = res[1].userTreeTo;
        }
      })
      .catch(error => this.alert.error(error));
  }

  capplication() {
    this.navbarService.getApplication()
      .then(res => {
        if (res[0]) {
          this.authority = res[1].userTreeTo;
        }
      })
      .catch(error => this.alert.error(error));
  }

  cuser() {
    this.navbarService.getUser()
      .then(res => {
        if (res[0]) {
          this.authority = res[1].userTreeTo;
        }
      })
      .catch(error => this.alert.error(error));
  }
}
